<template>
  <el-drawer
    v-model="drawerVisible"
    :size="650"
    :title="titleMap[mode]"
    @closed="$emit('closed')"
    append-to-body
  >
    <el-container v-loading="saveLoading">
      <el-main>
        <el-form
          :model="form"
          :rules="rules"
          ref="saveForm"
          label-width="140px"
        >
          <el-form-item label="企业名称" prop="enterprise_id">
            <el-input
              v-model="form.enterprise_id"
              placeholder="请输入企业名称"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="缴纳人数" prop="personCount">
            <el-input-number
              v-model="form.personCount"
              controls-position="right"
              :min="0"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="用工人数" prop="employeeCount">
            <el-input-number
              v-model="form.employeeCount"
              controls-position="right"
              :min="0"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="排序" prop="orderNum">
            <el-input-number
              v-model="form.orderNum"
              controls-position="right"
              :min="0"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="联系电话" prop="contact">
            <el-input
              v-model="form.contact"
              placeholder="请输入联系电话"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </el-main>
      <el-footer>
        <div class="form-footer">
          <el-button type="" @click="close">取消</el-button>
          <el-button type="primary" @click="submit">提交</el-button>
        </div>
      </el-footer>
    </el-container>
  </el-drawer>
</template>

<script>
export default {
  name: "commisioner-save",
  emits: ["success", "closed"],
  data() {
    return {
      saveLoading: false,
      drawerVisible: false,
      mode: "add",
      titleMap: {
        add: "新增企业",
        edit: "编辑企业",
      },
      tip: "添加成功",
      form: {
        id: "",
        enterprise_id: "",
        industry: "",
        block: "",
        orderNum: "",
		address:""
      },
      rules: {
        // commisionerName: [{ required: true, message: "请输入企业名称" }],
      },
    };
  },
  watch: {},
  mounted() {},
  methods: {
    //打开
    open(mode = "add") {
      this.mode = mode;
      this.drawerVisible = true;
      return this;
    },
    //修改
    async edit(id) {
      this.tip = "修改成功";
      this.drawerVisible = true;
      this.saveLoading = true;
      let params = { id: id };
      let res = await this.$API.commisioner.details.get(params);
      if (res.code == 0) {
        this.form = res.data;
        this.saveLoading = false;
      } else {
        let errdata = this.$TOOL.response(res);
        this.$alert(
          this.$TOOL.json.get("", errdata, "message"),
          this.$TOOL.json.get("", errdata, "title"),
          {
            type: "error",
            confirmButtonText: "确定",
            dangerouslyUseHTMLString: true,
            showCancelButton: false,
          }
        )
          .then(() => {
            this.drawerVisible = false;
          })
          .catch(() => {});
      }
    },
    //提交
    submit() {
      this.$refs.saveForm.validate(async (valid) => {
        if (valid) {
          let res = {};
          if (this.mode == "add") {
            res = await this.$API.commisioner.save.post(this.form);
          }else{
            res = await this.$API.commisioner.update.post(this.form);
          }
          if (res.code == 0) {
            this.$message.success(this.tip);
            //调用父类刷新
            this.$emit("onRefresh");
            this.drawerVisible = false;
          } else {
            let errdata = this.$TOOL.response(res);
            this.$alert(
              this.$TOOL.json.get("", errdata, "message"),
              this.$TOOL.json.get("", errdata, "title"),
              {
                type: "error",
                confirmButtonText: "确定",
                dangerouslyUseHTMLString: true,
                showCancelButton: false,
              }
            );
          }
        }
      });
    },
    //表单注入数据
    setData(data) {
      Object.assign(this.form, data);
    },
    // 关闭页面
    close() {
      this.$emit("closed");
    },
  },
};
</script>

<style></style>
